﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;


namespace GetJSON
{
    public partial class Form1 : Form
    {
        /// <summary>
        ///  剧情文件路径
        /// </summary>
        private string path;

        private string eventName;

        private string changeBankName;

        private string oldBankName;


        public Form1()
        {
            InitializeComponent();
        }

        private void btn_SelectPath_Click(object sender, EventArgs e)
        {
            // 选文件夹目录

            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
            folderBrowserDialog.ShowDialog();
            //fileDialog.Multiselect = true;
            //fileDialog.Title = "请选择文件";
            //fileDialog.Filter = "所有文件(*json*)|*.json*"; //设置要选择的文件的类型
            text_Path.Text = folderBrowserDialog.SelectedPath;
            path = text_Path.Text;
        }

        private void btn_out_Click(object sender, EventArgs e)
        {
            // 执行的时候
            eventName = text_EventName.Text;
            changeBankName = text_bankName.Text;

            if (string.IsNullOrEmpty(path))
            {
                path = text_Path.Text;
            }

            changeBankName = string.Format("bundleName=\"{0}\"", changeBankName);

            oldBankName = this.text_oldBank.Text;
            oldBankName = string.Format("bundleName=\"{0}\"", oldBankName);

            if (string.IsNullOrEmpty(eventName) == false && string.IsNullOrEmpty(changeBankName) == false)
            {
                this.GetFile(path);

            }
            
            MessageBox.Show("替换完成");

        }


        private void GetFile(string path)
        {

            // 遍历路径下json 
            // 对比name 如果一样
            // 修改里面 的bundleName 然后保存
            DirectoryInfo TheFolder = new DirectoryInfo(path);

            DirectoryInfo[] curAllNextFolder = TheFolder.GetDirectories();

            foreach (var NextFolder in curAllNextFolder)
            {

                DirectoryInfo[] directory = NextFolder.GetDirectories();
                FileInfo[] fileInfos = NextFolder.GetFiles();
                if (fileInfos.Length > 0)
                {
                    foreach (var file in fileInfos)
                    {
                        this.ChangeFileName(file.FullName);
                    }
                }
                if (directory.Length > 0 )
                {
                    foreach (var item in directory)
                    {
                        this.GetFile(item.FullName);
                    }
                }
            }

            FileInfo[] curfileInfos = TheFolder.GetFiles();
            if (curfileInfos.Length > 0)
            {
                foreach (var file in curfileInfos)
                {
                    this.ChangeFileName(file.FullName);
                }
            }
          
        }

        private void ChangeFileName(string path)
        {
            if (string.IsNullOrEmpty(path) == false && path.Contains(".json") && path.Contains(".meta") == false)
            {
                //Console.Write(path);
                // 读取当前文件
                string jsonFile = path;

                //using (System.IO.StreamReader file = System.IO.File.OpenText(jsonFile))
                //{
                //    using (JsonTextReader reader = new JsonTextReader(file))
                //    {
                //        JObject o = (JObject)JToken.ReadFrom(reader);
                //        var name = o["name"].ToString();
                //        Console.Write(name);
                //    }
                //}
                StringBuilder outToStr = new StringBuilder();
                using (var fs = new FileStream(jsonFile, FileMode.Open))
                {
                    int fsLen = (int)fs.Length;
                    byte[] txtByte = new byte[fsLen];
                    int r = fs.Read(txtByte, 0, txtByte.Length);
                    string myStr = System.Text.Encoding.UTF8.GetString(txtByte);
                    //string[] curRowStr = myStr.Split('\r', '\n','\t');

                    

                    StringBuilder resultStr = new StringBuilder();
                    int startIndex = 0;
                    int endIndex = 0;
                    string cur = string.Empty;
                    bool isStart = false;
                    for (int i = 0; i < myStr.Length; i++)
                    {
                        cur = myStr.Substring(i,1);
                        if (cur == "{")
                        {
                            //startIndex = i;
                            startIndex = i;
                            isStart = true;
                        }
                        endIndex++;

                        if (isStart)
                        {
                            resultStr.Append(cur);
                        }
                        else
                        {
                            outToStr.Append(cur);
                        }

                        if (cur == "}")
                        {
                            // 取 resultStr 遍历
                            if (resultStr.ToString().Contains(this.eventName))
                            {
                                //string oldBank = this.text_bankName.Text;
                                //string bundle = string.Format("bundleName=\"{0}\"", oldBank);
                                resultStr.Replace(oldBankName, this.changeBankName);
                            }

                            outToStr.Append(resultStr);
                            endIndex = 0;
                            isStart = false;
                            resultStr.Clear();
                        }
                    }
                    fs.Close();
                    fs.Dispose();
                }

                using (System.IO.StreamWriter file = new System.IO.StreamWriter(jsonFile, false))
                {

                    file.Write(outToStr);
                    file.Close();
                    file.Dispose();
                }
            }
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
